package com.example.demoJDBC.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description :
 * author :song
 * create :2022-05-11 09:39:00
 */
public class ListAvg {
    public static void main(String[] args) {
        List<BigDecimal> list = new ArrayList<>();
        //给对象赋值
        list.add(new BigDecimal(-10));
        list.add(new BigDecimal(30));
        list.add(new BigDecimal(-40));
        list.add(new BigDecimal(20));
        list.add(new BigDecimal(-100));
        list.add(new BigDecimal(50));

        List<BigDecimal> collect1 = list.stream().sorted(Comparator.comparing(s -> Math.abs(s.doubleValue()))).collect(Collectors.toList());

        System.out.println(collect1);

        BigDecimal collect = new BigDecimal(list.stream().collect(Collectors.averagingDouble(BigDecimal::doubleValue)));
        System.out.println("平均数为： " + collect);


        BigDecimal avgNewBrnchCd = list.stream().reduce(BigDecimal.ZERO, BigDecimal :: add).divide(BigDecimal.valueOf(list.size()), 2, BigDecimal.ROUND_HALF_UP);
        System.out.println("该数据总条数为：" + list.size());
        System.out.println("平均数为： " + avgNewBrnchCd);
    }

}
